package com.b.controller;

import java.util.List;
import java.util.Map;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.b.domain.Order;
import com.b.service.OrderService;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;


/**
 * 订单表(order)表控制层
 *
 * @author xxxxx
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    /**
     * 服务对象
     */
    @Resource
    private OrderService orderService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Order selectOne(String id) {
        return orderService.selectByPrimaryKey(id);
    }
    @GetMapping("list")
    public Page<Order> list(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "userId", required = false) Integer userId,
            @RequestParam(value = "productId", required = false) Integer productId,
            @RequestParam(value = "status", required = false) String status
    ) {
        PageRequest pageRequest = PageRequest.of(page-1 , pageSize );
        return orderService.list(userId, productId,status, pageRequest);
    }

    @GetMapping("/recent5daysAmount")
    public List<Map<String, Object>> recent5daysAmount() {
        return orderService.getRecent5DaysAmount();
    }


    //删除订单
    @PostMapping("/delete/{id}")
    public Boolean deleteOrder(@PathVariable ("id") String id) {
        System.out.println("id = " + id + " 类型: " + (id == null ? "null" : id.getClass().getName()));
        return orderService.deleteByPrimaryKey(id);
    }

    //修改订单状态
    @PostMapping("/updateStatus")
    public Boolean updateStatus(@RequestParam("id") String id, @RequestParam("status") String status){
        return orderService.updateStatus(id, status);

    }


    //    @PostMapping("/createOrder")
//    public Boolean createOrder( @RequestParam("userId") Integer userId,
//                                @RequestParam("productId") Integer productId,
//                                @RequestParam("quantity") Integer quantity
//
//                                ) {
//        return orderService.createOrder(userId, productId, quantity);
//    }


}
